using Game.Main.Model;
using Game.XH;
using SimpleJSON;
using UnityEngine;

namespace Game.Component
{
	public class SuitcaseItem : BaseItem
	{
		public enum State
		{
			Close = 0,
			Open = 1
		}

		private State state;

		public override void Init(ItemArchiveData data)
		{
			base.Init(data);
			SetState((State)data.state);
		}

		public override void QuicklyTouch(TouchEventData touchEventData)
		{
			base.QuicklyTouch(touchEventData);
			if (state == State.Close)
			{
				SetState(State.Open);
			}
			else
			{
				SetState(State.Close);
			}
		}

		private void SetState(State val)
		{
			if (state == val)
			{
				return;
			}
			state = val;
			model.Find("Open").SetObjectActive(state == State.Open);
			frontMask.SetObjectActive(state == State.Open);
			model.Find("Close").SetObjectActive(state == State.Close);
			if (platforms != null)
			{
				for (int i = 0; i < platforms.Count; i++)
				{
					platforms[i].gameObject.SetActive(state == State.Open);
				}
			}
			ChangeBoxColl(model.GetComponent<BoxCollider2D>(), model.Find("CloseColl").GetComponent<BoxCollider2D>());
		}

		private void ChangeBoxColl(BoxCollider2D coll, BoxCollider2D target)
		{
			Vector2 offset = coll.offset;
			Vector2 size = coll.size;
			coll.offset = target.offset;
			coll.size = target.size;
			target.offset = offset;
			target.size = size;
		}

		public override void PackData(JSONObject obj)
		{
			base.PackData(obj);
			obj["33"] = (int)state;
		}
	}
}
